﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Tools.Viewers.MeshViewer
{
    public partial class FormMeshChooser : Form
    {
        public List<ResourcePath> ResourcePaths;
        public string MeshName
        {
            get
            {
                FileInfo file = new FileInfo(MeshFileName);
                if (file != null && file.Extension.ToLower() == ".mesh")
                {
                    return file.Name;
                }
                return null;
            }
        }

        public string MeshFileName
        {
            get
            {
                return textBoxBrowser.Text;
            }
            set
            {
                //meshSelected = false;
                buttonOpenMesh.Enabled = false;
                buttonAdd.Enabled = false;
                buttonRemove.Enabled = false;
                if (value != null && File.Exists(value))
                {
                    FileInfo file = new FileInfo(value);
                    if (file != null && file.Extension.ToLower() == ".mesh")
                    {
                        //meshSelected = true;
                        buttonOpenMesh.Enabled = true;
                        buttonAdd.Enabled = true;
                        buttonRemove.Enabled = true;

                        treeResources.Nodes.Clear();
                        addAutoResources(file.DirectoryName);
                    }
                }
                
                textBoxBrowser.Text = value;
            }
        }

        //private bool meshSelected;
        //public bool MeshSelected
        //{
        //    get
        //    {
        //        return meshSelected;
        //    }
        //}

        public FormMeshChooser()
        {
            InitializeComponent();
            ResourcePaths = new List<ResourcePath>();
            MeshFileName = null;
        }

        public FormMeshChooser(string fileName)
        {
            InitializeComponent();
            ResourcePaths = new List<ResourcePath>();
            MeshFileName = fileName;
            if (buttonOpenMesh.Enabled)
            {
                buttonBrowser.Enabled = false;
            }
        }

        private void addAutoResources(string directoryPath)
        {
            DirectoryInfo cur = new DirectoryInfo(directoryPath);
            DirectoryInfo parent = cur.Parent;
            // if directory is root folder, then only add the base directory to resources
            // else, add parent folder (and all children folders of the parent folder) to resources 
            if (cur.Parent == null)
            {
                addResources(cur);
            }
            else
            {
                addResources(cur.Parent);

                DirectoryInfo[] dirs = parent.GetDirectories();
                for (int index = 0; index < dirs.Length; index++)
                {
                    addResources(dirs[index]);
                }
            }
        }

        private void addResources(DirectoryInfo dir)
        {
            string subDirectoryPath = dir.FullName;

            ResourcePaths.Add(new ResourcePath(subDirectoryPath, "Folder"));
            treeResources.Nodes.Add("(Folder) - " + subDirectoryPath);

            FileInfo[] files = dir.GetFiles();
            for (int index = 0; index < files.Length; index++)
            {
                string file = files[index].FullName;
                string extension = files[index].Extension;
                if (extension.ToLower() == ".zip")
                {
                    ResourcePaths.Add(new ResourcePath(file, "ZipFile"));
                    treeResources.Nodes.Add("(ZipFile) - " + file);
                }
            }
        }

        //private void refreshTreeResources()
        //{
        //    TreeView
        //}

        private void buttonBrowser_Click(object sender, EventArgs e)
        {
            browserOpenMesh.FileName = MeshFileName;
            if (browserOpenMesh.ShowDialog() == DialogResult.OK)
            {
                MeshFileName = browserOpenMesh.FileName;
            }
        }

        private void buttonOpenMesh_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.OK;
            Close();
        }

        private void buttonAddFolder_Click(object sender, EventArgs e)
        {
            if (browserAddFolder.ShowDialog() == DialogResult.OK)
            {
                ResourcePaths.Add(new ResourcePath(browserAddFolder.SelectedPath, "Folder"));
                treeResources.Nodes.Add("(Folder) - " + browserAddFolder.SelectedPath);
            }
        }

        private void buttonAddZipFile_Click(object sender, EventArgs e)
        {
            if (browserAddZipFile.ShowDialog() == DialogResult.OK)
            {
                ResourcePaths.Add(new ResourcePath(browserAddZipFile.FileName, "ZipFile"));
                treeResources.Nodes.Add("(ZipFile) - " + browserAddZipFile.FileName);
            }
        }

        private void buttonRemoveSelected_Click(object sender, EventArgs e)
        {
            if (treeResources.SelectedNode != null)
            {
                int index = treeResources.SelectedNode.Index;
                ResourcePaths.RemoveAt(index);
                treeResources.Nodes.RemoveAt(index);
            }
        }

        private void buttonRemoveAll_Click(object sender, EventArgs e)
        {
            ResourcePaths.Clear();
            treeResources.Nodes.Clear();
        }
    }
}